package com.tbit.uqbike.client.remote.zk;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @Author: chen
 * @Description: 中科支付回调
 * @Date: 创建于 16:06 2022/3/11
 */
public class CodecUtil {

    public static final String ALGORITHM_AES = "AES";
    public static final String CHARSET = "UTF-8";
    private static final String ALGORITHM_CIPHER_AES = "AES/ECB/PKCS5Padding";

    /**
     * AES算法加密数据
     *
     * @param data 待加密的明文数据
     * @param key  AES密钥字符串
     * @return AES加密后的经过Base64编码的密文字符串, 加密过程中遇到异常则抛出RuntimeException
     */
    public static String buildAESEncrypt(String data, String key) {
        try {
            // 实例化Cipher对象,它用于完成实际的加密操作
            Cipher cipher = Cipher.getInstance(ALGORITHM_CIPHER_AES);
            // 还原密钥,并初始化Cipher对象,设置为加密模式
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(key), ALGORITHM_AES));
            // 执行加密操作,加密后的结果通常都会用Base64编码进行传输
            // 将Base64中的URL非法字符如'+','/','='转为其他字符,详见RFC3548
            return Base64.encodeBase64URLSafeString(cipher.doFinal(data.getBytes(CHARSET)));
        } catch (Exception e) {
            throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
        }
    }


    /**
     * AES算法解密数据
     *
     * @param data 待解密的经过Base64编码的密文字符串
     * @param key  AES密钥字符串
     * @return AES解密后的明文字符串, 解密过程中遇到异常则抛出RuntimeException
     */
    public static String buildAESDecrypt(String data, String key) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM_CIPHER_AES);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(key), ALGORITHM_AES));
            return new String(cipher.doFinal(Base64.decodeBase64(data)), CHARSET);
        } catch (Exception e) {
            throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
        }
    }


    public static void main(String[] args) {

    /*PayPush payPush = new PayPush();
    payPush.setPayNo("GXBIKE3323213213123");
    payPush.setOperNo("1");
    payPush.setPayType(1);
    payPush.setOrderType(0);
    *//**充值结束订单需要传递车辆编号*//*
    if(ZkConstant.ORDER_TYPR_RETURNBIKE.equals()){
      RideLog rideLog = rideLogService.getByUserIdNotFinish(user.getUserId());
      payPush.setOrderNo(rideLog.getOrderNO());
      payPush.setBikeNo(rideLog.getUserCode());
    }
    payPush.setAmount(BigDecimal.valueOf(money/100));
    payPush.setBrand(user.getAccountId().toString());
    payPush.setUserPhone(user.getPhone());
    payPush.setRegisterTime(DateTimeUtil.stringFormatyyyyMMddHHmmss(user.getJoinTime()));
    payPush.setOrderTime(DateTimeUtil.getDaformatyyyyMMddHHmmss(new Date()));
    payPush.setAppId(appId);
    payPush.setOpenId(openId);
    payPush.setClientIp(ip);
    payPush.setNotify_url(zkPayConfig.getNotifyUrl());
    String requestData = CodecUtil.buildAESEncrypt(JSON.toJSONString(payPush),zkPayConfig.getScrect());
    RequestEntityZk requestEntityZk = new RequestEntityZk();
    requestEntityZk.setRequestData(requestData);
    requestEntityZk.setMgrId(zkPayConfig.getMgrId());
    String requestTime =DateTimeUtil.getDaformatyyyyMMddHHmmss(new Date());
    requestEntityZk.setFlowNo(zkPayConfig.getMgrId()+requestTime+ TenpayUtil.buildRandom(6));
    requestEntityZk.setRequestTime(requestTime);*/
        /**{mgrId='100001', flowNo='258703675889946624', requestTime='20220315140031', requestData='RolESowHdNkCyGqKb4PRvenJHHOtPVxtmpwI9Cduo6MYzxDrzLm-kBOOaAOnGJseo9KtY4xFs4K7VG_akdyQy_paTCYPRMwtC2GoS3UWALLOJ7UcpPKkzLFZR94eGm7B3Af69dSyLtiLTkMUIgn5Cg'}*/
        RequestEntityZk requestEntityZk = new RequestEntityZk();
        requestEntityZk.setMgrId(100001L);
        requestEntityZk.setFlowNo("258703675889946624");
        requestEntityZk.setRequestTime("20220315140031");
        requestEntityZk.setRequestData("RolESowHdNkCyGqKb4PRvenJHHOtPVxtmpwI9Cduo6MYzxDrzLm-kBOOaAOnGJseo9KtY4xFs4K7VG_akdyQy_paTCYPRMwtC2GoS3UWALLOJ7UcpPKkzLFZR94eGm7B3Af69dSyLtiLTkMUIgn5Cg");
        String json = JSON.toJSONString(requestEntityZk);
        String result = HttpRequest.post("http://clientnew.uqbike.com/zkPay/callback.do").header(Header.CONTENT_TYPE, "application/json").body(json).execute().body();
        System.out.println(result);
    }
}
